package mo.leetcode_hot;

import java.util.HashMap;
import java.util.Map;

public class L560 {

    public int subarraySum(int[] nums, int k) {
        int res = 0, pre = 0;
        Map<Integer, Integer> sum2count = new HashMap<>();
        sum2count.put(0, 1);
        for (int num : nums) {
            pre += num;
            res += sum2count.getOrDefault(pre-k, 0);
            sum2count.put(pre, sum2count.getOrDefault(pre, 0)+1);
        }
        return res;
    }

    public static void main(String[] args) {
        L560 test = new L560();
        System.out.println(test.subarraySum(new int[]{-1, -1, 1}, 0));
    }
}
